package com.xju.subjectsystem.service.impl;

import com.xju.subjectsystem.mapper.StudentMapper;
import com.xju.subjectsystem.pojo.LoginInfo;
import com.xju.subjectsystem.pojo.Student;
import com.xju.subjectsystem.service.StudentService;
import com.xju.subjectsystem.utils.JwtUtil;
import com.xju.subjectsystem.utils.Md5Util;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -*- coding:utf-8 -*-
 *
 * @projectName: Subjectsystem
 * @package: com.xju.subjectsystem.service.impl
 * @className: StudentServiceImpl
 * @author: Eiard
 * @description: TODO : 学生服务实现类
 * @date: 2024-12-15 19:58
 */
@Service
public class StudentServiceImpl implements StudentService {

    private final StudentMapper studentMapper;

    public StudentServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public Student selectByUsername(String username) {
        Student student = studentMapper.selectByPrimaryKey(username);

        if (student != null) {
            student.setSpwd(null);
        }

        return student;
    }

    @Override
    public void deleteByUsername(String username) {
        studentMapper.updateAccountStatusByPrimaryKey(username, 0);
    }

    @Override
    public void add(Student student) throws NoSuchAlgorithmException {
        student.setSpwd(Md5Util.encrypt(student.getSpwd()));
        studentMapper.insert(student);
    }

    @Override
    public void addSelective(Student student) throws NoSuchAlgorithmException {
        student.setSpwd(Md5Util.encrypt(student.getSpwd()));
        studentMapper.insertSelective(student);
    }

    @Override
    public Map<String, List<String>> batchAddStudents(List<Student> studentList) {
        List<String> successList = new ArrayList<>();
        List<String> failList = new ArrayList<>();

        for (Student student : studentList) {
            try {
                this.addSelective(student);
                successList.add("学生：" + student.getSname() + " 导入成功");
            } catch (Exception e) {
                failList.add("学生：" + student.getSname() + " 导入失败，原因：数据异常");
            }
        }
        // 将结果封装成Map返回
        Map<String, List<String>> resultData = new HashMap<>();
        resultData.put("successList", successList);
        resultData.put("failList", failList);

        return resultData;
    }

    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public void updateSelective(Student student) {
        studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    public void updateStudent(Student student) {
        Student student1 = new Student();
        student1.setSno(student.getSno());

        if (student.getSpwd() != null) {
            student1.setSpwd(student.getSpwd());
        }
        if (student.getHomeroom() != null) {
            student1.setHomeroom(student.getHomeroom());
        }
        if (student.getTel() != null) {
            student1.setTel(student.getTel());
        }
        if (student.getTmail() != null) {
            student1.setTmail(student.getTmail());
        }
        if (student.getWechat() != null) {
            student1.setWechat(student.getWechat());
        }
        if (student.getPicture() != null) {
            student1.setPicture(student.getPicture());
        }

        this.updateSelective(student1);

    }

    @Override
    public LoginInfo login(String username, String password) throws NoSuchAlgorithmException {
        // 账号不可能是纯数字,不会和手机号冲突
        Student studentByUsername = studentMapper.selectByPrimaryKey(username);
        Student studentByTel = studentMapper.selectByTel(username);

        // 必定是二选一
        Student student = studentByTel != null ? studentByTel : studentByUsername;

        // 如果未找到用户或者账号状态为禁用
        if (student == null || student.getAccountStatus() == 0) {
            return null;
        }

        // 验证密码
        if (student.getSpwd().equals(Md5Util.encrypt(password))) {
            // 构建JWT的Claims
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", student.getSno());
            claims.put("name", student.getSname());
            claims.put("role", "student");

            // 生成Token
            String token = JwtUtil.generateToken(claims);

            // 返回登录信息
            return new LoginInfo(student.getSno(), student.getSname(), token);
        }

        return null;
    }
}
